package com.example.aidl_test.server;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.example.aidl_test.common.Book;
import com.example.aidl_test.common.IBookManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class MyService extends Service {

    private static final String TAG = "aidl_test_service";

    public MyService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mBookList.add(new Book(1, "Book1"));
        mBookList.add(new Book(2, "Book2"));
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind");
        return mBinder;
    }

    // TODO 线程安全
    private final List<Book> mBookList = new CopyOnWriteArrayList<>();

    private final Binder mBinder = new IBookManager.Stub(){
        @Override
        public List<Book> getBookList() throws RemoteException {
            Log.i(TAG, "getBookList");
            return mBookList;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            Log.i(TAG, "addBook, book:" + book);
            mBookList.add(book);
        }
    };
}